/**
 * Copyright (c) 2010 icer He <icerhe@gmail.com>
 */
package me.linkcn.ydtxl.client.activity;

import com.fsck.k9.activity.Accounts;

import me.linkcn.ydtxl.client.Consts;
import me.linkcn.ydtxl.client.R;
import me.linkcn.ydtxl.client.util.ContactListItemAdapter;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.Toast;

/**
 * @author icer.He
 *
 */
public class FavActivity extends Activity {
	
	private GridView favGrid;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(Consts.TAG, "FavActivity create");
		show();
	}
	
	private void show() {
		setContentView(R.layout.fav_grid);
		
    	//绑定按钮
    	//主画面
	    final Button main = (Button) findViewById(R.id.main);
	    main.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {		
				Intent intent = new Intent(FavActivity.this, MainActivity.class);
				startActivity(intent);
			}
        });
	    
	    //多选画面
	    final Button multi = (Button) findViewById(R.id.multi);
	    multi.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {		
				Intent intent = new Intent(FavActivity.this, MultiActivity.class);
				startActivity(intent);
			}
        });
	    
	    
	    //邮件(暂时)
	    final Button mail = (Button)findViewById(R.id.mail);
	    mail.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {		
				Intent intent = new Intent(FavActivity.this, Accounts.class);
				startActivity(intent);
			}
        });		
		
		favGrid = (GridView)findViewById(R.id.fav_grid);
		
		String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
        + Contacts.HAS_PHONE_NUMBER + "=1) AND ("
        + Contacts.DISPLAY_NAME + " != '' )) AND ("
        + Contacts.STARRED + "=1)";
        
		Cursor c =
	        getContentResolver().query(Contacts.CONTENT_URI, Consts.CONTACTS_SUMMARY_PROJECTION, select,
	        null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
		startManagingCursor(c);
	    ContactListItemAdapter adapter = new ContactListItemAdapter(this, R.layout.fav_item, c);
	    favGrid.setAdapter(adapter);
	}
	
	private static final int PERFERENCES = 0; //设置
	private static final int ABOUT = 1; //关于
	private static final int EXIT = 2; //退出
    /**
     * Menu菜单
     */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
        menu.add(0, PERFERENCES, 1, R.string.menu_preferences);
        menu.add(0, ABOUT,       2, R.string.menu_about);
        menu.add(0, EXIT,       3, R.string.menu_exit);
		return true;
	}
	
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case PERFERENCES:
                //startActivity(new Intent(this, SetAlarm.class));
            	Toast.makeText(getApplicationContext(), "功能尚未完成",
            		     Toast.LENGTH_SHORT).show();
                return true;
            case ABOUT:
                showDialog(ABOUT_DIALOG);
                return true;
            case EXIT:
            	showDialog(EXIT_DIALOG);
            	return true;
            default:
                return false;
        }
    }
    
	private static final int ABOUT_DIALOG = 0; //关于对话框
	private static final int EXIT_DIALOG = 1; //退出对话框
    
    @Override
    protected Dialog onCreateDialog(int id) {
    	switch(id) {
    	case ABOUT_DIALOG:
            return new AlertDialog.Builder(FavActivity.this)
            .setTitle(R.string.menu_about)
            .setMessage(R.string.text_about)
            .setNeutralButton(R.string.button_ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked Something so do some stuff */
                }
            })
            .create();
    	case EXIT_DIALOG:
    		return new AlertDialog.Builder(FavActivity.this)
            .setTitle(R.string.menu_exit)
            .setMessage(R.string.text_exit)
            .setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    finish();
                }
            })
            .setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked Cancel so do some stuff */
                }
            })
            .create();
    	}
    	return null;
    }

}
